#!/bin/bash -x

echo 'proxy=MARK_YUM_PROXY' >> /etc/yum.conf
sync

export edk2_repo_dir=MARK_EDK2_REPO_DIR
export edk2_source_tree_basename=MARK_EDK2_SOURCE_TREE_BASENAME
export edk2_source_tree_tarball=MARK_EDK2_SOURCE_TREE_TARBALL

stage=${1:-1}

if [ $stage -eq 1 ]; then
  yum install -y rpm-build dnf-plugins-core git

  echo '%_sourcedir MARK_EDK2_REPO_DIR' > ~/.rpmmacros

  pushd ${edk2_repo_dir}
  yum-builddep -y edk2.spec
  popd # ${edk2_repo_dir}
fi

if [ $stage -eq 2 ]; then
  pushd ${edk2_repo_dir}
  rm -rf ~/rpmbuild || true
  rpmbuild -bp edk2.spec
  cd ~/rpmbuild/BUILD/${edk2_source_tree_basename}/
  git init -q && \
  git config user.name rpm-build && \
  git config user.email rpm-build && \
  git config core.whitespace cr-at-eol && \
  git config am.keepcr true && \
  git add -A . && \
  git commit -q -m 'MARK_EDK2_REPO_BRANCH commit@MARK_EDK2_REPO_COMMIT base'
  cd ../
  tar -zcf ${edk2_source_tree_tarball} ${edk2_source_tree_basename}

  popd # ${edk2_repo_dir}
fi
